package cn.antblog.jvm.juc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.*;

/**
 * @ClassName TestAtomic
 * @Description 原子类
 * @Author YangJingyi
 * @Date 2023/8/2 16:02
 * @Version 1.0
 */
public class TestAtomic {

//    public static void main(String[] args) {
////        int i = 1;
////        System.out.println(i++);
//        AtomicInteger i = new AtomicInteger(1);
//        System.out.println(i.getAndIncrement());  //如果想实现i += 2这种操作，可以使用 addAndGet() 自由设置delta 值
//    }

//    private static AtomicInteger i = new AtomicInteger(0);
//    public static void main(String[] args) throws InterruptedException {
//        Runnable r = () -> {
//            for (int j = 0; j < 100000; j++)
//                i.getAndIncrement();
//            System.out.println("自增完成！");
//        };
//        new Thread(r).start();
//        new Thread(r).start();
//        TimeUnit.SECONDS.sleep(1);
//        System.out.println(i.get());
//    }

//    public static void main(String[] args) throws InterruptedException {
//        LongAdder adder = new LongAdder();
//        Runnable r = () -> {
//            for (int i = 0; i < 100000; i++)
//                adder.add(1);
//        };
//        for (int i = 0; i < 100; i++)
//            new Thread(r).start();   //100个线程
//        TimeUnit.SECONDS.sleep(1);
//        System.out.println(adder.sum());   //最后求和即可
//    }

    /**
     * 性能对比
     * @param args
     * @throws InterruptedException
     */
//    public static void main(String[] args) throws InterruptedException {
//        System.out.println("使用AtomicLong的时间消耗："+test2()+"ms");
//        System.out.println("使用LongAdder的时间消耗："+test1()+"ms");
//    }
//
//    private static long test1() throws InterruptedException {
//        CountDownLatch latch = new CountDownLatch(100);
//        LongAdder adder = new LongAdder();
//        long timeStart = System.currentTimeMillis();
//        Runnable r = () -> {
//            for (int i = 0; i < 100000; i++)
//                adder.add(1);
//            latch.countDown();
//        };
//        for (int i = 0; i < 100; i++)
//            new Thread(r).start();
//        latch.await();
//        return System.currentTimeMillis() - timeStart;
//    }
//
//    private static long test2() throws InterruptedException {
//        CountDownLatch latch = new CountDownLatch(100);
//        AtomicLong atomicLong = new AtomicLong();
//        long timeStart = System.currentTimeMillis();
//        Runnable r = () -> {
//            for (int i = 0; i < 100000; i++)
//                atomicLong.incrementAndGet();
//            latch.countDown();
//        };
//        for (int i = 0; i < 100; i++)
//            new Thread(r).start();
//        latch.await();
//        return System.currentTimeMillis() - timeStart;
//    }

    /**
     * 引用类型原子操作
     */
    public static void main(String[] args) throws InterruptedException {
//        String a = "Hello";
//        String b = "World";
//        AtomicReference<String> reference = new AtomicReference<>(a);
//        reference.compareAndSet(a, b);
//        System.out.println(reference.get());
        String a = "Hello";
        String b = "World";
        AtomicStampedReference<String> reference = new AtomicStampedReference<>(a, 1);  //在构造时需要指定初始值和对应的版本号
        reference.attemptStamp(a, 2);   //可以中途对版本号进行修改，注意要填写当前的引用对象
        System.out.println(reference.compareAndSet(a, b, 2, 3));   //CAS操作时不仅需要提供预期值和修改值，还要提供预期版本号和新的版本号
    }


}
